part of "../file_system.dart";

extension DirectoryExtra on Directory {
  /// 获取基本信息，比如子文件夹、名字等
  String get name => basename(path);
  Iterable<File> get subFiles => listSync().whereType<File>();
  Iterable<Directory> get subDirectories => listSync().whereType<Directory>();
  File subFile(String fullname) => File(join(path, fullname));
  Directory subDir(String name) => Directory(join(path, name));

  /// 在当前文件夹下新建子文件夹
  Directory createSubDirectory(String name) {
    final subDir = Directory("$path/$name");
    if (!subDir.existsSync()) subDir.createSync(recursive: true);
    return subDir;
  }

  File createSubFile(String nameWithExtension) {
    final subFile = File("$path/$nameWithExtension");
    if (!subFile.existsSync()) subFile.createSync(recursive: true);
    return subFile;
  }

  void initIf() {
    // 如果文件夹不存在，则创建；如果已经存在，则没有任何动作
    if (!existsSync()) {
      createSync(recursive: true);
      print("文件夹$path不存在，首次创建文件夹");
    }
  }

  void deleteIf() {
    if (existsSync()) deleteSync(recursive: true);
  }
}
